﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;
using utk = Microsoft.Office.Interop.Outlook;


namespace HY.WEB.OA
{
    public partial class _msglist : PageBase
    {

        public string whe = "",stype="";
        public string us,nm, tab = "msg_list";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            nm = Session["user"].ToString();
            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request["stype"]))
                {
                    stype = Request["stype"].Trim();
                    hidstype.Text=stype;
                }
                else
                    return;


                btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
                btnDelete2.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
               // btallsend.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");

                
                //btnNew.OnClientClick = Window1.GetSaveStateReference(hidstype.ClientID)
                //+Window1.GetShowReference("hrad_add.aspx?stype=" + stype + "", "新增");
                btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
      
                BindGrid();
                fill(); //填充查询
                
            }

            if (stype == "") stype= hidstype.Text;

            if (stype == "3")  //删除
            {
                btnDelete2.Visible = true; 
                btnDelete.Visible = false;
            }
            else
            {
                btnDelete.Visible = true;
                btnDelete2.Visible = false;
            }

          
        }

      

          private void fill()
          {
             DataTable d=BLL.BLLfun.get_search(tab);
             for (int i = 0; i < d.Rows.Count; i++)
               {
                   ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
               }
               ddlfiled.DataBind();

               DataTable e = BLL.BLLfun.get_where();
               for (int i = 0; i < e.Rows.Count; i++)
               {
                   ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
               }

                ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));  
          


         }

          public string getnm(string nmid)
          {
              BLL.HR.sys_user u = new BLL.HR.sys_user();
              return BLL.BLLfun.getname(nmid);
          }


        private void BindGrid()
        {
            DataTable table;            

           
                table = BLL.msg.msgbase.get_list(us,stype,whe,nm);
           

            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table;

       
                table = BLL.msg.msgbase.get_list(us, stype, whe,nm);
 

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            

            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

 


        #region Events

        //发送
        protected void btnsend_Click(object sender, EventArgs e)
        {

            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
               {
                 sb.Append(Grid1.DataKeys[row][0].ToString());
                 sb.Append(",");
               }

            // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
          //  HY.BLL.msg.msgbase.xsend(sb.ToString().TrimEnd(',')); //模似删除
           
            BindGrid();

        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid1.PageIndex * Grid1.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row+val][0].ToString());
                sb.Append(",");

            }

           // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
            BindGrid();

        }

        protected void btnDelete2_Click(object sender, EventArgs e)
        {

            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid1.SelectedRowIndexArray)
            {
                sb.Append(Grid1.DataKeys[row][0].ToString());
                sb.Append(",");

            }

            // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            HY.BLL.msg.msgbase.DeleteList2(sb.ToString().TrimEnd(',')); //模似删除
            BindGrid();

        }


        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
              string key = txt.Text.Trim();
              stype = hidstype.Text;
              try
              {
                  if (key == "") whe = BLL.pub.pubwhere.get_where_sql(Session["ID"].ToString(), "hr_ad");
                  BindGrid();

              }catch(Exception ex)
              {
                  Alert.Show("查询条件错误");
              }
              finally
              {
                  whe = "";
              }
        }

        #endregion
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text.Trim();
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
              if (ddlf=="sid"||ddlf=="编号") 
                  whe = "" + ddlf + " " + ddlw + " " + key + "";
              else
                  whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }

      
    

        }




    
}